package com.gonsin.ta.logic.process;

import com.gonsin.ta.logic.exceptions.TaException;
import com.gonsin.ta.logic.parse.TaExecutorService;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class TaSignal {

    private TaExecutorService executorService;

    /**
     * 要接收的信号实体
     */
    private String instanceId;

    /**
     * 要发出的信号
     */
    private String signal;

    private Map<String, Serializable> contextValue;


    public TaSignal(TaExecutorService executorService, String instanceId, String signal) {
        this.executorService = executorService;
        this.instanceId = instanceId;
        this.contextValue = new HashMap<>();
        this.signal = signal;
    }

    /**
     * 添加变量
     */
    public TaSignal put(String key, Serializable value){
        contextValue.put(key, value);
        return this;
    }


    /**
     * 执行流程
     */
    public TaResponse post(){
        if(executorService == null){
            throw new TaException("TA初始化未完成，可以监听 TaInitAfterFinishedEvent 事件对TA初始化进度进行检测");
        }
        return executorService.postSignal(instanceId, signal, contextValue);
    }

}
